package com.compiler.lexico;

public class Character {
	private String characters;
	private boolean range = false;
	private boolean fields = false;
	
	public Character(String characters) {
		this.characters = characters;
		if(characters.contains("-")) this.range = true;
		if(characters.contains(";")) this.fields = true;
	}
	
	public Character(char c) {
		this.characters = new String();
		this.characters += (int)c;
	}
	
	public boolean contains(char c) {
		int character = (int)c;
		if(this.fields) {
			Integer r11,r12,r21,r22;
			r11 = Integer.parseInt(this.characters.split(";")[0].split("-")[0]);
			r12 = Integer.parseInt(this.characters.split(";")[0].split("-")[1]);
			r21 = Integer.parseInt(this.characters.split(";")[1].split("-")[0]);
			r22 = Integer.parseInt(this.characters.split(";")[1].split("-")[1]);
			if((character >= r11 && character <= r12) || (character >= r21 && character <= r22))
				return true;
		} else if(range) {
				Integer r11,r12;
				r11 = Integer.parseInt(this.characters.split("-")[0]);
				r12 = Integer.parseInt(this.characters.split("-")[1]);
				if(character >= r11 && character <= r12)
					return true;
		} else if(character == Integer.parseInt(characters)) return true;
		return false;
	}
	
	@Override
	public int hashCode() {
		if(this.characters.contains("-") || this.characters.contains(";") || this.characters.contains("+"))
			return this.characters.hashCode();
		return Integer.parseInt(this.characters); 
	}
	
	@Override
	public boolean equals(Object obj) {
		return this.characters.equals(((Character)obj).characters);
	}
	
	public String toString() {
		return this.characters;
	}
}
